package com.iqiyi.pps.epg.api.server.proxy;

import com.alibaba.fastjson.JSONObject;
import com.iqiyi.pps.epg.api.aop.annotation.SkipInterfaceLog;
import com.iqiyi.pps.epg.api.model.web.program.*;
import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.query.TResponse;
import com.iqiyi.pps.epg.api.server.web.program.ProgramDirectorWebService;
import com.iqiyi.pps.epg.api.utils.Constants;
import com.iqiyi.pps.epg.api.utils.PlayStatusUtil;
import com.iqiyi.pps.epg.api.utils.RegUtil;
import com.iqiyi.pps.epg.api.utils.ServiceUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.thrift.TException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by karkarrotxu on 2016/9/20.
 */
@Service
public class ProgramDirectorProxy extends BaseProxy {

    private static Logger logger = LoggerFactory.getLogger(ProgramDirectorProxy.class);

    public String resourceAdd(String directorId, String programQipuId, String signalLegoId,
                              String signalType, String signalId, String isBackup,
                              String isDefaultBackup, String signalName, String signalUrl,
                              String signalPreviewUrl) {
        String jsonStrResp = "";
        ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
        try {
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播台ID不能为空");
            }
            if (!RegUtil.checkProgramQipuId(programQipuId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "对应直播节目ID不能为空");
            }
            if (StringUtils.isBlank(signalType)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "信号类型不能为空");
            }
            if (StringUtils.isBlank(isBackup)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "是否灾备不能为空");
            }
            if (StringUtils.isBlank(signalName)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "信号名称不能为空");
            }
            if (StringUtils.isBlank(isDefaultBackup)) {
                isDefaultBackup = "0";
            }
            if (StringUtils.isBlank(signalLegoId)) {
                signalLegoId = "0";
            }
            String opUser = ServiceUtil.getOpUser();
            if (opUser == null) {
                return ServiceUtil.getResponseError("操作人员异常");
            }
            //     String opUser = "zhang";
            TProgramDirectorResource thriftEntity = new TProgramDirectorResource();
            thriftEntity.setDirectorId(Integer.parseInt(directorId));
            thriftEntity.setProgramQipuId(Long.parseLong(programQipuId));
            thriftEntity.setSignalType(Integer.parseInt(signalType));
            thriftEntity.setSignalId(signalId);
            thriftEntity.setIsBackup(Integer.parseInt(isBackup));
            thriftEntity.setIsDefaultBackup(Integer.parseInt(isDefaultBackup));
            thriftEntity.setSignalName(signalName);
            thriftEntity.setSignalUrl(signalUrl);
            thriftEntity.setSignalPreviewUrl(signalPreviewUrl);
            thriftEntity.setOpUser(opUser);
            thriftEntity.setSignalLegoId(Long.parseLong(signalLegoId));
            thriftEntity.setPlayCounts(1);
            int entityId = pdwService.addResourceEntity(thriftEntity);
            if (entityId > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, entityId, DETAIL_RESPONSE_SUC);
            } else if (entityId == -2) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, "自动创建预览流地址失败");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, "添加失败");
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.RESOURCE_ADD.getIndex(), null, entityId + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[resourceAdd]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String resourceEdit(String id, String isDefaultBackup, String signalName, String playStatus, String directorId) {
        String jsonStrResp = "";
        ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
        try {
            if (!RegUtil.isPositiveInteger(id)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播素材ID格式错误");
            }
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播ID格式错误");
            }
            if (StringUtils.isEmpty(isDefaultBackup)) {
                isDefaultBackup = "-1";
            }
            if (StringUtils.isEmpty(signalName)) {
                signalName = "";
            }
            if (StringUtils.isEmpty(playStatus)) {
                playStatus = "-1";
            }
            int result = pdwService.editResourceEntity(Integer.parseInt(id), Integer.parseInt(isDefaultBackup), signalName, Integer.parseInt(playStatus));
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, result, "修改成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, "修改失败");
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.RESOURCE_EDIT.getIndex(), null, result + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[resourceEdit]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String resourceDel(String id, String directorId) {
        String jsonStrResp = "";
        ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
        try {
            if (!RegUtil.isPositiveInteger(id)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播素材ID格式错误");
            }
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播ID格式错误");
            }
            int result = pdwService.delResourceEntity(Integer.parseInt(id));
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, result, "删除成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, "删除失败");
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.RESOURCE_DEL.getIndex(), null, result + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[resourceDel]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String resourceGetList(String directorId, String isBackup) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播台ID不能为空");
            }

            TFilter tFilter = new TFilter();
            Map<String, String> params = new HashMap<String, String>();
            params.put("EQI_directorId", directorId);
            if (RegUtil.isNonnegativeInteger(isBackup)) {
                params.put("EQI_isBackup", isBackup);
            }
            params.put("EQI_isDeleted", 0 + "");
            params.put("EQI_resourceType", "1");
            tFilter.setFilters(params);
            List<TProgramDirectorResource> result = pdwService.getDirectorResourceList(tFilter);
            if (result != null) {
                jsonStrResp = Constants.getResponseList(Constants.CODE_SUC, result, "查询成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "查询失败");
            }
        } catch (Exception e) {
            logger.error("[resourceGetList]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }

        return jsonStrResp;
    }

    public String resourceSwitch(String id, String directorId, String time, String playCounts) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (!RegUtil.isPositiveInteger(id)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播素材ID格式错误");
            }
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播台ID格式错误");
            }
            if (StringUtils.isBlank(playCounts)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播台播次不能为空");
            }
            long timeValue = 0L;
            if (time != null && RegUtil.isNonnegativeInteger(time)) {
                timeValue = Long.valueOf(time);
            }

            int result = pdwService.switchResourceEntity(Integer.parseInt(id), Integer.parseInt(directorId), timeValue, Integer.parseInt(playCounts));
            String codeStr = getCodeStr(result);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, result, "切换成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, codeStr);
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.DIRECTOR_SWITCH_RESOURCE.getIndex(), null, result + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[resourceSwitch]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String addDirector(String entity) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            TProgramDirector tProgramDirector = JSONObject.parseObject(entity, TProgramDirector.class);
            if (tProgramDirector == null) {
                return ServiceUtil.getResponseError("格式解析错误");
            }
            if (!RegUtil.checkProgramQipuId(tProgramDirector.getProgramQipuId() + "")) {
                return ServiceUtil.getResponseError("奇谱id格式错误");
            }
            if (!RegUtil.isDateFormat(tProgramDirector.getStartPlayTime()) || !RegUtil.isDateFormat(tProgramDirector.getStopPlayTime()) || tProgramDirector.getStartPlayTime().compareTo(tProgramDirector.getStopPlayTime()) > 0) {
                return ServiceUtil.getResponseError("时间格式错误");
            }
            if (tProgramDirector.getDelay() < 0 || tProgramDirector.getSource() < 1 || StringUtils.isBlank(tProgramDirector.getProgramName())) {
                return ServiceUtil.getResponseError("参数值错误");
            }
            String opUser = ServiceUtil.getOpUser();
            if (opUser == null) {
                return ServiceUtil.getResponseError("操作人员异常");
            }
            //          String opUser = "zhang";
            tProgramDirector.setOpUser(opUser);
            int entityId = pdwService.addDirectorEntity(tProgramDirector);
            if (entityId == 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "数据正常");
            } else if (entityId > 0) {
            //    writeDirectorLog(entityId, Constants.DirectorLogType.DIRECTOR_ADD.getIndex(), null, entityId + "|" + getStrParam());
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "添加成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, "添加失败");
            }
        } catch (Exception e) {
            logger.error("[addDirector]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String editDirector(String entity) {
        String jsonStrResp = "";
        try {
            TProgramDirector tProgramDirector = JSONObject.parseObject(entity, TProgramDirector.class);
            if (tProgramDirector == null) {
                return ServiceUtil.getResponseError("格式解析错误");
            }
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (tProgramDirector.getId() < 1) {
                return ServiceUtil.getResponseError(" 导播id格式错误");
            }
            if (!RegUtil.checkProgramQipuId(tProgramDirector.getProgramQipuId() + "")) {
                return ServiceUtil.getResponseError("奇谱id格式错误");
            }
            if (!RegUtil.isDateFormat(tProgramDirector.getStartPlayTime()) || !RegUtil.isDateFormat(tProgramDirector.getStopPlayTime()) || tProgramDirector.getStartPlayTime().compareTo(tProgramDirector.getStopPlayTime()) > 0) {
                return ServiceUtil.getResponseError("时间格式错误");
            }

            if (StringUtils.isBlank(tProgramDirector.getOutputUrl())) {
                return ServiceUtil.getResponseError("输出地址格式错误");
            }

            if (tProgramDirector.getDelay() < 0 || tProgramDirector.getDelay() > 30 || tProgramDirector.getSource() < 1) {
                return ServiceUtil.getResponseError("参数值错误");
            }
            String opUser = ServiceUtil.getOpUser();
            if (opUser == null) {
                return ServiceUtil.getResponseError("操作人员异常");
            }
            tProgramDirector.setOpUser(opUser);
            int ret = pdwService.editDirectorEntity(tProgramDirector);
            String codeStr = getCodeStr(ret);
            if (ret > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, codeStr);
            } else {
                jsonStrResp = ServiceUtil.getResponseFail(codeStr);
            }
            writeDirectorLog(tProgramDirector.getId(), Constants.DirectorLogType.DIRECTOR_EDIT.getIndex(), null, codeStr + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[editDirector]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String listDirector(String qipuId, String pname, String startDate, String endDate, String programStatus,
                               String playStatus, Integer pageNo, Integer pageSize, String fullMode
    ) {
        String jsonStrResp = "";
        pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
        pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;
        Map<String, String> params = new HashMap<>();
        if (RegUtil.checkProgramQipuId(qipuId)) {
            params.put("EQL_programQipuId", qipuId);
        }
        if (StringUtils.isNotEmpty(pname)) {
            params.put("LIKES_programName", pname);
        }
        if (RegUtil.isDateFormat(startDate)) {
            params.put("GET_startPlayTime", startDate);
        }
        if (RegUtil.isDateFormat(endDate)) {
            params.put("LET_startPlayTime", endDate);
        }
        if (RegUtil.isNonnegativeInteger(programStatus)) {
            PlayStatusUtil.addParamsByDirectorProgramStatus(params, programStatus);
        }
        if (RegUtil.isInteger(playStatus)) {
            params.put("EQI_playStatus", playStatus);
        } else {
            params.put("GEI_playStatus", "0");
        }

        TFilter tFilter = new TFilter();
        tFilter.setFilters(params);
        TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);

        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            TProgramDirectorPage tProgramDirectorPage = pdwService.getDirectorList(page, tFilter, Boolean.parseBoolean(fullMode));
            if (tProgramDirectorPage != null) {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tProgramDirectorPage, DETAIL_RESPONSE_SUC).toJsonString();
            } else {
                jsonStrResp = ServiceUtil.getResponseFail("查询失败");
            }
        } catch (Exception e) {
            logger.error("[listDirector]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String stopDirector(String directorId) {
        String jsonStrResp = "";

        if (!RegUtil.isNonnegativeInteger(directorId)) {
            return ServiceUtil.getResponseError("导播id格式错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            int result = pdwService.stopDirector(Integer.valueOf(directorId));
            String codeStr = getCodeStr(result);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, codeStr);
            } else {
                jsonStrResp = ServiceUtil.getResponseFail(codeStr);
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.DIRECTOR_STOP.getIndex(), null, codeStr + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[stopDirector]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String startDirector(String directorId) {
        String jsonStrResp = "";
        if (!RegUtil.isNonnegativeInteger(directorId)) {
            return ServiceUtil.getResponseError("导播id格式错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            int result = pdwService.startDirector(Integer.valueOf(directorId));
            String codeStr = getCodeStr(result);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, codeStr);
            } else {
                jsonStrResp = ServiceUtil.getResponseFail(codeStr);
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.DIRECTOR_START.getIndex(), null, codeStr + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[startDirector]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String setMainResource(String pid, String mainResourceId) {
        String jsonStrResp = "";
        if (StringUtils.isBlank(pid) || StringUtils.isBlank(mainResourceId)) {
            return ServiceUtil.getResponseError("参数错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            String opUser = ServiceUtil.getOpUser();
            int result = pdwService.updateDirectorMainResource(Long.parseLong(pid), Integer.parseInt(mainResourceId), opUser);
            String codeStr = getCodeStr(result);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, codeStr);
            } else {
                jsonStrResp = ServiceUtil.getResponseFail(codeStr);
            }
        } catch (Exception e) {
            logger.error("[startDirector]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String setResourcePlayCounts(String id, Integer playCounts) {
        String jsonStrResp = "";
        if (StringUtils.isBlank(id) || playCounts == null) {
            return ServiceUtil.getResponseError("参数错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            int result = pdwService.updateResourcePlayCounts(Integer.parseInt(id), playCounts);
            String codeStr = getCodeStr(result);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, codeStr);
            } else {
                jsonStrResp = ServiceUtil.getResponseFail(codeStr);
            }
        } catch (Exception e) {
            logger.error("[setResourcePlayCounts]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog
    public String getLogList(String directorId, Integer pageNo, Integer pageSize) {
        String jsonStrResp = "";
        pageNo = (pageNo == null || pageNo < 1) ? 1 : pageNo;
        pageSize = (pageSize == null || pageSize < 1) ? 20 : pageSize;
        if (!RegUtil.isPositiveInteger(directorId)) {
            return ServiceUtil.getResponseError("导播id格式错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            TFilter tFilter = new TFilter();
            Map<String, String> params = new HashMap<String, String>();
            params.put("EQI_directorId", directorId);
            tFilter.setFilters(params);
            TPage page = new TPage(-1, pageNo, pageSize, "id", "desc", true);
            TProgramDirectorLogPage tProgramDirectorLogPage = pdwService.getLogList(page, tFilter);
            if (tProgramDirectorLogPage == null) {
                jsonStrResp = ServiceUtil.getResponseFail(DETAIL_ERROR_EXCEPTION);
            } else {
                jsonStrResp = Constants.getResponse(Constants.CODE_SUC, tProgramDirectorLogPage, DETAIL_RESPONSE_SUC).toJsonString();
            }
        } catch (Exception e) {
            logger.error("[startDirector]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String switchBackUp(String directorId) {
        String jsonStrResp = "";
        if (!RegUtil.isPositiveInteger(directorId)) {
            return ServiceUtil.getResponseError("导播id格式错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            int result = pdwService.switchToBackUp(Integer.valueOf(directorId));
            String codeStr = getCodeStr(result);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, codeStr);
            } else {
                jsonStrResp = ServiceUtil.getResponseError(codeStr);
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.DIRECTOR_SWITCH_BACKUP.getIndex(), null, codeStr + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[switchBackUp]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String delDirector(String directorId) {
        if (!RegUtil.isPositiveInteger(directorId)) {
            return ServiceUtil.getResponseError("导播id格式错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            TResponse result = pdwService.delDirector(Integer.valueOf(directorId));
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.DIRECTOR_DEL.getIndex(), null, result.getMsg() + "|" + getStrParam());
            if (Constants.CODE_SUC.equals(result.getCode())) {
                return Constants.getJsonResponse(Constants.CODE_SUC, null, result.getMsg());
            } else {
                return ServiceUtil.getResponseFail(result.getMsg());
            }
        } catch (Exception e) {
            logger.error("[delDirector]", e);
            return ServiceUtil.getResponseError();
        }
    }

    public String releaseDirector(String directorId) {
        if (!RegUtil.isPositiveInteger(directorId)) {
            return ServiceUtil.getResponseError("导播id格式错误");
        }
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            TResponse result = pdwService.releaseDirector(Integer.valueOf(directorId));
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.DIRECTOR_DEL.getIndex(), null, result.getMsg() + "|" + getStrParam());
            if (Constants.CODE_SUC.equals(result.getCode())) {
                return Constants.getJsonResponse(Constants.CODE_SUC, null, result.getMsg());
            } else {
                return ServiceUtil.getResponseFail(result.getMsg());
            }
        } catch (Exception e) {
            logger.error("[releaseDirector]", e);
            return ServiceUtil.getResponseError();
        }
    }

    public String effectAdd(String data) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            TProgramDirectorEffect tProgramDirectorEffect = JSONObject.parseObject(data, TProgramDirectorEffect.class);
            if (tProgramDirectorEffect == null) {
                return ServiceUtil.getResponseError("数据格式错误");
            }
            String opUser = ServiceUtil.getOpUser();
            if (opUser == null) {
                return ServiceUtil.getResponseError("操作人员异常");
            }
            tProgramDirectorEffect.setOpUser(opUser);
            int entityId = pdwService.addEffect(tProgramDirectorEffect);
            if (entityId == 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "数据正常");
            } else if (entityId > 0) {
                writeDirectorLog(entityId, Constants.DirectorLogType.DIRECTOR_ADD.getIndex(), null, entityId + "|" + getStrParam());
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "添加成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, "添加失败");
            }
            writeDirectorLog(tProgramDirectorEffect.getDirectorId(), Constants.DirectorLogType.EFFECT_ADD.getIndex(), null, entityId + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[effectAdd]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    @SkipInterfaceLog(needParams = true)
    public String effectEdit(int id, String url, String name, String location, String extraInfo, String status, String directorId,
                             int isPreview) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (id <= 0) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "特效ID错误");
            }
            String opUser = ServiceUtil.getOpUser();
            if (opUser == null) {
                return ServiceUtil.getResponseError("操作人员异常");
            }

//            if(StringUtils.isNotEmpty(location) && !RegUtil.isJsonString(location)) {
//                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "位置信息不是有效的JSON字符串");
//            }

//            if(StringUtils.isNotEmpty(extraInfo) && !RegUtil.isJsonString(extraInfo)) {
//                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "额外参数信息不是有效的JSON字符串");
//            }

            if (null == url) {
                url = "";
            }

            if (null == name) {
                name = "";
            }

            if (null == location) {
                location = "";
            }

            if (null == extraInfo) {
                extraInfo = "";
            }

            if (null == status) {
                status = "";
            }

            int result = pdwService.editEffect(id, url, name, location, extraInfo, status, isPreview, opUser);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, result, "修改成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, "修改失败");
            }
            if (isPreview == 0) {
                String params = getStrParam();
                if (params == null) {
                    String.format("id=%d,url=%s,name=%s,location=%s,extraInfo=%s,status=%s,isPreview=%d",
                            id, url, name, location, extraInfo, status, isPreview);
                }
                writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.EFFECT_EDIT.getIndex(), null, result + "|" + params);
            }
        } catch (TException e) {
            logger.error("[effectEdit] e = " + e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String effectDelete(int id, String directorId) {
        String jsonStrResp = "";
        ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
        try {
            if (id <= 0) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "特效ID错误");
            }
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播ID格式错误");
            }
            int result = pdwService.deleteEffect(id);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, result, "删除成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, "删除失败");
            }
            writeDirectorLog(Integer.valueOf(directorId), Constants.DirectorLogType.EFFECT_DELETE.getIndex(),
                    null, result + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[effectDelete]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String effectList(String directorId, int type, String status) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播台ID不能为空");
            }

            if (StringUtils.isNotEmpty(status) && !StringUtils.isNumeric(status)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "状态参数必须为数字");
            }

            TFilter tFilter = new TFilter();
            Map<String, String> params = new HashMap<String, String>();
            params.put("EQI_directorId", directorId);
            if (type > 0) {
                params.put("EQI_type", type + "");
            }
            if (StringUtils.isEmpty(status)) {
                params.put("GEI_status", "0");
            } else {
                params.put("EQI_status", status);
            }
            tFilter.setFilters(params);
            List<TProgramDirectorEffect> result = pdwService.getEffectList(tFilter);
            if (result != null) {
                jsonStrResp = Constants.getResponseList(Constants.CODE_SUC, result, "查询成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "查询失败");
            }
        } catch (Exception e) {
            logger.error("[effectList]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }

        return jsonStrResp;
    }

    public String commentaryAdd(String name, String programQipuId, String directorId, String streamUrl) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (StringUtils.isBlank(name)) {
//                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "解说名称不能为空");
                name = "";
            }
            if (!RegUtil.checkProgramQipuId(programQipuId)) {
                return ServiceUtil.getResponseError("节目id格式错误");
            }
            if (!RegUtil.isPositiveInteger(directorId)) {
                return ServiceUtil.getResponseError("导播id格式错误");
            }

            TProgramDirectorCommentary entity = new TProgramDirectorCommentary();
            entity.setName(name);
            entity.setProgramQipuId(Long.valueOf(programQipuId));
            entity.setStreamUrl(StringUtils.isBlank(streamUrl) ? "" : streamUrl);
            entity.setDirectorId(Integer.valueOf(directorId));

            int entityId = pdwService.addCommentary(entity);
            if (entityId == 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "数据正常");
            } else if (entityId > 0) {
//                writeDirectorLog(entityId, Constants.DirectorLogType.DIRECTOR_ADD.getIndex(), null, entityId + "|" + getStrParam());
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, entityId, "添加成功");
            } else if (entityId == -998) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, "连接超出上限, 添加失败");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, entityId, "添加失败");
            }
            writeDirectorLog(Integer.parseInt(directorId), Constants.DirectorLogType.COMMENTARY_ADD.getIndex(), null, entityId + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[commentaryAdd]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String commentaryEdit(String name, int id, String programQipuId, String directorId, String streamUrl,
                                 String volumn, String status) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (id <= 0) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "解说ID错误");
            }
            if (!RegUtil.checkProgramQipuId(programQipuId)) {
                return ServiceUtil.getResponseError("节目id格式错误");
            }

            if(StringUtils.isNotBlank(status) && !RegUtil.isNonnegativeInteger(status)) {
                return ServiceUtil.getResponseError("status格式错误");
            }

            if(StringUtils.isBlank(name)) {
                name = "";
            }
            if(StringUtils.isBlank(streamUrl)) {
                streamUrl = "";
            }
            if(!RegUtil.isPositiveInteger(volumn)) {
                volumn = "";
            }
            if(StringUtils.isBlank(status)) {
                status = "-2";
            }

            int result = pdwService.editCommentary(id, name, Long.valueOf(programQipuId), streamUrl, volumn,
                    Integer.valueOf(status));
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, null, "修改成功");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, "修改失败");
            }
            writeDirectorLog(Integer.parseInt(directorId), Constants.DirectorLogType.COMMENTARY_EDIT.getIndex(), null,
                    result + "|" + getStrParam());
        } catch (TException e) {
            logger.error("[commentaryEdit] e = " + e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String commentaryDel(int id, String directorId) {
        String jsonStrResp = "";
        ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
        try {
            if (id <= 0) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "解说ID错误");
            }
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播ID格式错误");
            }
            int result = pdwService.deleteCommentary(id);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, result, "删除成功");
            }else if (result == -997) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, "解说正在播出，无法删除!");
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, "删除失败");
            }
            writeDirectorLog(Integer.parseInt(directorId), Constants.DirectorLogType.COMMENTARY_DELETE.getIndex(), null,
                    result + "|" + getStrParam());
        } catch (Exception e) {
            logger.error("[commentaryDel]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }
        return jsonStrResp;
    }

    public String commentaryList(String directorId, String programQipuId, int id) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (!RegUtil.isPositiveInteger(directorId)) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播台ID不能为空");
            }
            if (!RegUtil.checkProgramQipuId(programQipuId)) {
                return ServiceUtil.getResponseError("节目id格式错误");
            }

            TFilter tFilter = new TFilter();
            Map<String, String> params = new HashMap<String, String>();
            params.put("EQI_directorId", directorId);
            params.put("EQL_programQipuId", programQipuId);
            if(id > 0) {
                params.put("EQI_id", id + "");
            }
            params.put("EQI_resourceType", "2");
            params.put("GEI_status", "0");

            tFilter.setFilters(params);
            List<TProgramDirectorCommentary> result = pdwService.getCommentaryList(tFilter);
            if (result != null) {
                jsonStrResp = Constants.getResponseList(Constants.CODE_SUC, result, "查询成功").toJsonString();
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, null, "查询失败");
            }
        } catch (Exception e) {
            logger.error("[commentaryList]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }

        return jsonStrResp;
    }

    public String setDirectorResourceId(long programQipuId, int playResouceId, int downResourceId) {
        String jsonStrResp = "";
        try {
            ProgramDirectorWebService.Iface pdwService = ServiceUtil.getpdwService();
            if (programQipuId <= 0) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "导播台节目qipuID参数错误");
            }
            if (playResouceId <= 0 || downResourceId <= 0) {
                return Constants.getJsonResponse(Constants.CODE_ERROR, null, "资源ID参数错误");
            }
            int result = pdwService.setDirectorResourceId(programQipuId, playResouceId, downResourceId);
            String codeStr = getCodeStr(result);
            if (result > 0) {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_SUC, result, codeStr);
            } else {
                jsonStrResp = Constants.getJsonResponse(Constants.CODE_ERROR, result, codeStr);
            }
        } catch (Exception e) {
            logger.error("[commentaryList]", e);
            jsonStrResp = ServiceUtil.getResponseError();
        }

        return jsonStrResp;
    }
}
